//
//  NRWNavigationView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/7.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWNavigationView.h"
@interface NRWNavigationView()
@property (nonatomic, strong) UIImageView *mainImage;
@end

@implementation NRWNavigationView
{
    //config
    UIColor *_mainColor;
    
    ////leftItem
    NSString *_leftImageName;
    UIColor *_leftTitleColor;
    CGFloat _leftTitleSize;
    
    ////rightItem
    UIColor *_rightTitleColor;
    CGFloat _rightTitleSize;
    
    ////titleItem
    UIColor *_titleColor;
    CGFloat _titleSize;
    
    //UI
    UIButton *_leftBtn;
    UIButton *_rightBtn;
    UILabel *_titleLabel;
    
    CGFloat _margin;//边距
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setupConfig];
        
        [self setupUI];
        
        [self layoutUI];
    }
    return self;
}

- (void)setupConfig {
    
    _mainColor = [UIColor whiteColor];
    
    _leftImageName = @"nav_back";
    _leftTitleColor = getColor(@"#323232");
    _leftTitleSize = 13;
    
    _rightTitleSize = 13;
    _rightTitleColor = getColor(@"#323232");
    
    _margin = 18;
    
    _titleColor = getColor(@"#323232");
    _titleSize = 17;
}

- (void)setupUI {
    
    self.backgroundColor = _mainColor;
    
    if ([UIScreen mainScreen].bounds.size.height == 812) {//iphone X
        
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 84);
    } else {
        
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
    }
    self.mainImage = DPImageView();
    self.mainImage.frame = self.frame;
    self.mainImage.backgroundColor = [UIColor clearColor];
    [self addSubview:self.mainImage];
    
    _leftBtn = [[UIButton alloc] init];
    [_leftBtn setTitleColor:_leftTitleColor forState:UIControlStateNormal];
    _leftBtn.titleLabel.font = [UIFont systemFontOfSize:_leftTitleSize];
    if (_leftImageName) {
        [_leftBtn setImage:[UIImage imageNamed:_leftImageName] forState:UIControlStateNormal];
    }
    [_leftBtn sizeToFit];
    //由于按钮点击范围太小, 影响体验   换成手势处理点击事件
    _leftBtn.userInteractionEnabled = NO;
//    [_leftBtn addTarget:self action:@selector(leftBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_leftBtn];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = _titleColor;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:_titleSize];
    [self addSubview:_titleLabel];
    
    _rightBtn = [[UIButton alloc] init];
    [_rightBtn setTitleColor:_rightTitleColor forState:UIControlStateNormal];
    _rightBtn.titleLabel.font = [UIFont systemFontOfSize:_rightTitleSize];
    [_rightBtn sizeToFit];
    [_rightBtn addTarget:self action:@selector(rightBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightBtn];
    
    self.layer.shadowOffset = CGSizeMake(0, 0.1);
    self.layer.shadowOpacity = 0.1;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.delaysTouchesBegan = YES;
    [self addGestureRecognizer:tap];
}

- (void)layoutUI {
    
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(self->_margin);
        make.bottom.equalTo(self.mas_bottom);
        make.height.equalTo(@44);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.centerX.equalTo(self.mas_centerX);
        make.width.equalTo(@150);
        make.height.equalTo(@44);
    }];
    
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-self->_margin);
        make.bottom.equalTo(self.mas_bottom);
        make.height.equalTo(@44);
        make.width.equalTo(@44);
    }];
}

- (void)setLeftTitle:(NSString *)title image:(NSString *)image {
    
    if (isEmptyString(title) && isEmptyString(image)) {
        _leftBtn.hidden = YES;
        return;
    }
    
    _leftBtn.hidden = NO;
    
    _leftBtn
    .setNormalTitle(title)
    .setNormalImage(getImage(image));
}

-(void)setNavBackviewImage:(NSString *)imageName{
    [self.mainImage setImage:[UIImage imageNamed:imageName]];
}
- (void)setRightTitle:(nullable NSString *)title image:(nullable NSString *)image {
    
    if (isEmptyString(title) && isEmptyString(image)) {
        _rightBtn.hidden = YES;
        return;
    }
    
    _rightBtn.hidden = NO;
    
    [_rightBtn setTitle:title forState:UIControlStateNormal];
    if (isNotEmptyString(image)) {
        [_rightBtn setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
}

- (void)leftBtnDidClick:(UIButton *)sender {

    if ([self.delegate respondsToSelector:@selector(navigationViewLeftItemDidClick)]) {

        [self.delegate navigationViewLeftItemDidClick];
    }
}

- (void)rightBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(navigationViewRightItemDidClick)]) {
        
        [self.delegate navigationViewRightItemDidClick];
    }
}

//手势方法, 根据范围确定点击返回   代替按钮响应事件
- (void)tapAction:(UITapGestureRecognizer *)sender {
    
    CGPoint touchPoint = [sender locationInView:self];
    
    if (touchPoint.x < 60) {
        
        if ([self.delegate respondsToSelector:@selector(navigationViewLeftItemDidClick)]) {
            
            [self.delegate navigationViewLeftItemDidClick];
        }
    }
}

- (void)setTitle:(NSString *)title {
    
    _title = title;
    
    _titleLabel.text = title;
}

@end
